﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHDLL;
using thinger.MTHModels;

namespace thinger.MTHProject
{
    public partial class FrmAlarm : Form
    {
        public FrmAlarm()
        {
            InitializeComponent();
            this.Load += FrmHistory_Load;
        }

        private SysLogManage sysLogManage = new SysLogManage();


        private void FrmHistory_Load(object sender, EventArgs e)
        {
            this.cbbAlarmType.DataSource = new List<string>() {"全部" ,"触发", "解除"};
            this.cbbAlarmType.SelectedIndex = 0;
            this.dtpStart.Value = DateTime.Now.AddDays(-1f);
            this.dtpStart.Value = DateTime.Now;
            this.dgvAlarmList.AutoGenerateColumns = false;

        }

        private void btnByTime_Click(object sender, EventArgs e)
        {
            string alarmType = this.cbbAlarmType.Text.Trim();
            string start = this.dtpStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string end = this.dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");

            //校验
            if (alarmType == null || alarmType.Length == 0)
            {
                new FrmMsgBoxWithOutAck("报警类型不能为空", "请检查").Show();
            }

            if (start == null || start.Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("开始时间不能为空", "请检查").Show();
            }

            if (end == null || end.Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("结束时间不能为空", "请检查").Show();
            }

            if (Convert.ToDateTime(end) < Convert.ToDateTime(start))
            {
                new FrmMsgBoxWithOutAck("结束时间不能小于开始时间", "请检查").Show();
            }
            TimeSpan timeSpan = Convert.ToDateTime(end) - Convert.ToDateTime(start);

            double totalDays = timeSpan.TotalDays;

            if (totalDays > 1)
            {
                new FrmMsgBoxWithOutAck("查询时间不能超过1天", "请检查").Show();
            }
            //多线程
            Task<List<SysLog>> task = Task.Run(() =>
            {
                List<SysLog> sysLogs = new List<SysLog>();
                if (alarmType.Equals("全部"))
                {
                    sysLogs =sysLogManage.SysLogAll();
                }
                else 
                {
                    sysLogs = sysLogManage.SysLogByTime(alarmType, start, end);
                }
                

                if (sysLogs == null || sysLogs.Count <= 0)
                {
                    new FrmMsgBoxWithOutAck("报警数据查询失败", "请检查MySql").Show();
                }
                return sysLogs;
            });
            Task task2 = task.ContinueWith(task1 =>
            {
                this.Invoke(new Action(() =>
                {
                    //刷新
                    Refresh(task1.Result);

                }));

            });

            

        }

        private void Refresh(List<SysLog> sysLogs)
        {
            if (sysLogs != null && sysLogs.Count > 0) 
            {
                this.dgvAlarmList.DataSource = null;
                this.dgvAlarmList.DataSource = sysLogs;
            }

        }

        private void btnOutExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //筛选文件
            saveFileDialog.Filter = "XLSX文件(*.xlsx)|*.xlsx|所有文件|*.*";

            saveFileDialog.Title = "导出历史报警";
            //扩展名
            saveFileDialog.DefaultExt = "xlsx";

            saveFileDialog.FileName = "历史报警" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //是否使用扩展名字
            saveFileDialog.AddExtension = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //保存
                MiniExcel.SaveAs(saveFileDialog.FileName, this.dgvAlarmList.DataSource);

                DialogResult dialogResult = new FrmMsgBoxWithAck("导出完成", "是否打开文件").ShowDialog();

                if (dialogResult == DialogResult.OK) 
                { 
                    //打开文件
                    Process.Start(saveFileDialog.FileName);
                }
            }
        }
    }
}
